package com.ljd.cmn.controller;

import com.ljd.cmn.Service.DictService;
import com.ljd.yygh.model.cmn.Dict;
import com.ljd.yygh.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@CrossOrigin
@Api(tags = "后台数据字典")
@RestController
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    DictService dictService;
    @ApiOperation(value = "根据ID查询子节点")
    @GetMapping("getDictById/{id}")
    public Result getDictById(@PathVariable("id") Long id) {
        List<Dict> dictList = dictService.getDictById(id);
        return Result.ok(dictList);
    }
    @ApiOperation(value = "导出数据")
    @GetMapping("exportData")
    public void exportData(HttpServletResponse response) {
        dictService.exportData(response);
    }
    @ApiOperation(value = "导入数据")
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{parentDictCode}/{value}")
    public String getName(@PathVariable("parentDictCode") String parentDictCode,
                          @PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue(parentDictCode,value);
    }
    @ApiOperation(value = "获取数据字典名称")
    @GetMapping(value = "/getName/{value}")
    public String getName(@PathVariable("value") String value) {
        return dictService.getNameByParentDictCodeAndValue("",value);
    }
}
